package ntd.util.frame.action.ai;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.config.common.enums.protocol.EnumStreamUrlProtocol;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.haikang.remote.RemoteHaiKang;
import ntd.util.frame.haikang.tool.ToolHaiKangVideo;
import ntd.util.frame.javacv.ComVisionApi;
import ntd.util.frame.javacv.HaikangRtspBean;
import ntd.util.frame.javacv.remote.AiVisionRemote;

import java.util.HashMap;
import java.util.Map;

public class ActionAiVideoUrl extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("AI实时流获取", "ai-get-realtime-url", new ActionAiVideoUrl());
    }

    @Override
    public ActionOutBo handle() {
        

        // 执行逻辑
        String cameraIndexCode = actionInBo.getParamStr("cameraIndexCode");
        String aiModeType = actionInBo.getParamStr("aiModeType");

        //验证参数
        if (StrUtil.isEmpty(cameraIndexCode) || StrUtil.isEmpty(aiModeType)) {
            throw new ComException("参数有误");
        }

        RemoteHaiKang remoteHaiKang = ToolHaiKangVideo.initHaiKang("40289f5a7e3c994d017e3c998daf0000");

        String url = remoteHaiKang.getStreamUrlByCameraIndexCode(cameraIndexCode, EnumStreamUrlProtocol.PROTOCOL_RTSP.getProtocolName());

        HaikangRtspBean bean = new Gson().fromJson(url, HaikangRtspBean.class);

        if (bean.getData() == null || StrUtil.isEmpty(bean.getData().getUrl())) {
            throw new ComException(200, "此站点摄像头无实时视频");
        }

        Map<String, String> result = new HashMap<>();
        //视频流地址http://47.92.119.61:8082/vision?model=Person&source=

        AiVisionRemote aiVisionRemote = ComVisionApi.initAiRemote().get(0);

        String videoUrl = aiVisionRemote.analysisUrl(aiModeType, bean.getData().getUrl());

        result.put("url", videoUrl);

        return new ActionOutBo(result);
    }
}
